"use strict";

$(function () {
    $(".modal .close").click(function () {
        $(".modal").addClass("hide");
    });
    $.ajax({
            type: "post",
            url: "/wzmanage/port/queryAll",
            contentType: "application/json;charset=utf-8",
            success: function success(data) {
            $.ajax({
                type: "post",
                url: "/wzmanage/channel/queryAll",
                contentType: "application/json;charset=utf-8",
                success: function success(da) {
                    console.log(da)
                    changeP2C(data,da)

                }
            })
        }
    })

    // loadPord(port)
    // selectPort("/wzmanage/revise/listThree")
    // updateRevise("/wzmanage/revise/updateThree");
    //一周/三天的页签切换
    function changeP2C(port,channel){
        $(".reviseInfo ").unbind()
        loadPord(port)
        selectPort("/wzmanage/revise/queryPortFore");
        updateRevise("/wzmanage/revise/addPortFore","/wzmanage/revise/updatePortFore");
        $(".reviseInfo ul.weekinfo").on("click", "li", function () {
            if($(this).hasClass("active"))return;
            var index = $(this).index();
            var html=$(".check-info button").html()
            if(html=="保存"){
                alert("请先保存")
                return
            }
            $(this).addClass("active").siblings().removeClass("active");
            if (index == 0) {
                loadPord(port)
                selectPort("/wzmanage/revise/queryPortFore")
                updateRevise("/wzmanage/revise/addPortFore","/wzmanage/revise/updatePortFore");
            } else {
                loadPord(channel)
                selectPort("/wzmanage/revise/queryChannelFore")
                updateRevise("/wzmanage/revise/addChannelFore","/wzmanage/revise/updateChannelFore");
            }
        });
    }

    //加载列表
    function loadList(url) {
        $.ajax({
            type: "post",
            url: url,
            contentType: "application/json;charset=utf-8",
            success: function success(data) {
                var data = data;
                if (data instanceof Array) {
                    tableContentSeven(data)
                    var index = 0;
                    var lng = data.length;
                    var num = 1;
                    var n = lng % 6 == 0 ? lng / 6 : parseInt(lng / 6) + 1;
                    // loadHtml(data, index, n, num);
                    clickLoad(data, index, n, num);
                }
            }
        });
    }
    function clickLoad(data, index, n, num) {
        $(".user-list tfoot").unbind();
        $(".user-list tfoot").on("click", "button", function (e) {
            e.preventDefault();
            if ($(this).hasClass('next')) {
                index = index + 6;
                num++;
                num >= n ? $(this).prop("disabled", 'true') : '';
            } else {
                if (num > 1) {
                    $(this).parent().siblings().find("button").prop("disabled", false);
                    index = index - 6 > 0 ? index - 6 : 0;
                    num--;
                    // num=num>=n?n:num
                }
            }
            loadHtml(data, index, n, num);
        });
    }
    //是否显示预报
    function updateRevise(url,urlupdate) {
        $(".check-info").unbind();

        $(".check-info").on("click", "button", function () {
            var that = $(this);
            var ht = $(this).html();
            var tr= $(".check-info tbody tr")
            var arr=[];
            var urls=''
            if (ht == "修改" || ht == "已修改") {
                tr.find(".reviseInp").prop("disabled",false).css("border","1px solid #eee")
                $(this).html("保存");
            } else if (ht == "保存" || ht == "已修改") {
                console.log(tr)
                for(var i=0;i<tr.length;i++){
                    var id = $(tr[i]).find("[name='id']").val();
                    var station= $(tr[i]).find("[name=station]").val();
                    var hours= $(tr[i]).find("[name=hours]").val();
                    var pr24= $(tr[i]).find("[name=pr24]").val();
                    var time= $(tr[i]).find("[name=time]").val();
                    var t=$(tr[i]).find("[name=t]").val();
                    var windd= $(tr[i]).find("[name=windd]").val();
                    var winds= $(tr[i]).find("[name=winds]").val();
                    var ww12= $(tr[i]).find("[name=ww12]").val();
                    arr.push({
                        id: id,
                        station: station,
                        time:time,
                        hours:hours,
                        pr24:pr24,
                        t: t,
                        windd:  windd,
                        winds: winds,
                        ww12: ww12,
                    })
                }
                var stationName=$("#selRain p span" ).html()
                var obj={stationName:stationName,dsBeans:arr}

                if($(tr[0]).find("[name='id']").val()=='null'){
                    urls=url;
                }else{
                    urls=urlupdate
                }
                console.log($(tr[0]).find("[name='id']").val())
                console.log(obj)


                $.ajax({
                    type: "post",
                    url: urls,
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(obj),
                    success: function success(data) {
                        console.log(data);
                        if (data == "OK") {
                            // ht.html("")
                            // loadList(url)
                            tr.find(".reviseInp").prop("disabled",false).css("border-color","transparent")
                            $(".new-add").addClass("hide");
                            $(".add-success h3").html("修改成功");
                            $(".add-success").css("display", "block");
                            that.html("修改");
                            setTimeout(function () {
                                $(".add-success").fadeOut();
                            }, 500);
                        }
                    }
                });
            }
        });
    }
    //判断是否为checked
    function ischecked(roleList, lis) {
        for (var j = 0; j < roleList.length; j++) {
            if (roleList[j].id == lis) {
                return 'checked';
            }
        }
        return " ";
    }

    function loadHtml(data, index, n, num) {
        var lis = num * 6 < data.length ? num * 6 : data.length;
        for (var i = 0 + index, html; i < lis; i++) {
            html += "<tr>\n                            \n                                    <td class=\"numbers\">" + (i + 1) + "<input class=\"id\"  type=\"hidden\" value=\"" + data[i].id + "\"></td>\n                                \n                                    <td class=\"publishTime rev\">" + data[i].time + "</td> \n                                    <td class=\"content rev\">\n                                    " +"<div>"+ (data[i].warning ? "<p>警告:" + data[i].warning + "</p>" : '') + "\n                                    <span>" + data[i].content + "</span></div>\n              \n                                    </td> \n                                    <td class=\"updaterole\">\n                                        <button class=\"btn btn-primary btn-large updateRes\"  data-toggle=\"" + data[i].checked + "\">" + (data[i].checked == 1 ? "已修改" : "修改") + "</button>\n                                        \n                                     \n                                    </td>         \n                                </tr>";
        }
        $(".user-list tbody").html(html);
        $(".user-list tfoot span").html('当前第' + num + '页,共' + n + '页');
    }
    //选择航道或港区，显示不同的预报
    function selectPort(url) {
        var station= $('.selectList ul li').eq(0).attr("i")
            var url1=url+"?station="+station
        loadList(url1);
        $(".selectList").unbind()
        $(".selectList").mouseenter(function () {
            $(this).find(".rain-cn").removeClass("hide")
        })
        $(".selectList").on("click", "li", function () {
            station = $(this).attr("i")
           var url2=url+"?station="+station
            loadList(url2);
            $(".selectList p span").html($(this).html())
            $(this).parent().parent().addClass("hide")


        })
    }
    //生成不同的港区
    function loadPord(data){
        for(var i=0,html='';i<data.length;i++){
            html+='<li i="'+data[i].stationId+'">'+data[i].name+'</li>'
        }
        $(".rain-cn ul").html(html)
        $("#selRain p span").html(data[0].name)
    }
    function tableContentSeven(list) {
        var html = "";
          for(var i=0;i<list.length;i++){
             var time= list[i].time?list[i].time:"暂无"
              html+='<tr>' +
                  '<td>'+(i+1)+'</td>'+
                  '<td ><input type="text" name="station" disabled value="'+list[i].station+'"><input name="id" type="hidden" value="'+list[i].id+'"><input name="hours" type="hidden" value="'+list[i].hours+'"></td>' +
                  '<td ><input  type="text" name="time" disabled value="'+time+'"></td>' +
                  '<td ><input class="reviseInp" type="text" name="pr24" disabled value="'+list[i].pr24+'"></td>' +
                  '<td ><input class="reviseInp" type="text" name="t" disabled value="'+list[i].t+'"></td>' +
                  '<td ><input class="reviseInp" type="text" name="windd" disabled value="'+list[i].windd+'"></td>' +
                  '<td ><input class="reviseInp" type="text" name="winds" disabled value="'+list[i].winds+'"></td>' +
                  '<td ><input class="reviseInp" type="text" name="ww12" disabled value="'+list[i].ww12+'"></td>' +
                  '</tr>'
          }

        $(".user-list tbody").html(html);
    }
    //插件
    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
});